package top.appx.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import top.appx.dao.UserDao;
import top.appx.entity.UserEntity;
import top.appx.exception.MsgException;
import top.appx.service.UserService;

import java.util.List;

/**
 * Created by lhzxd on 2017/5/3.
 */
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public UserEntity findById(String id) {
        return userDao.findById(id);
    }

    @Override
  //  @Cacheable(value = "ttvalue")
    public List<UserEntity> query() {
        System.out.println("invoke query method");
        return userDao.query();
    }

    @Override
    public void addUser(UserEntity userEntity) {
        userDao.add(userEntity);
    }

    @Override
    public void updateUser(UserEntity userEntity) {
        int r = userDao.update(userEntity);
        if(r==0){
            throw new MsgException(HttpStatus.CONFLICT,"用户不存在或已被删除");
        }
        System.out.println("更新影响行数:"+r);
    }

    @Override
    public void delUser(String id) {
        int r = userDao.delById(id);
        if(r==0){
            throw new MsgException(HttpStatus.NOT_FOUND,"该用户不存在或已被删除("+id+")");
        }
    }

    @Override
    public void updateUserPatch(UserEntity userEntity) {
        userDao.updateUserPatch(userEntity);
    }
}
